package com.itheima.stonepuzzle;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class MainFrame extends JFrame implements KeyListener {
    //图片编号数组
    private int[][] data = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0},
    };

    /**
     * 构造方法，创建对象调用
     */
    public MainFrame() {
        //添加键盘事件监听
        this.addKeyListener(this);
        //初始化窗体
        initFrame();
        //初始化数据
        initData();
        //绘制游戏界面
        paintView();

        setVisible(true);//设置窗体可见
    }

    /**
     * 此方法用于初始化数据（打乱二维数组）
     */
    public void initData() {
        //准备Random对象
        Random r = new Random();
        //遍历数组
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                int randomX = r.nextInt(4);
                int randomY = r.nextInt(4);

                int temp = data[i][j];
                data[i][j] = data[randomX][randomY];
                data[randomX][randomY] = temp;

            }
        }

    }

    /**
     * 此方法用于初始化窗体
     */
    public void initFrame() {
//        JFrame jFrame = new JFrame(); //创建窗体对象
        setSize(514, 595);//设置窗体大小
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//窗体默认关闭模式
        setTitle("石头迷阵单机版V1.0"); //设置窗体标题
        //setAlwaysOnTop(true);//设置窗体在最上层
        setLocationRelativeTo(null); //设置窗体居中显示

        setLayout(null); //取消默认布局

    }

    /**
     * 用于绘制游戏界面
     */
    public void paintView() {
        for (int i = 0; i < 4; i++) {
            for (int i1 = 0; i1 < 4; i1++) {
                JLabel imageLabel = new JLabel(new ImageIcon("D:\\document\\JAVA_Project\\Practise\\day04\\src\\com\\itheima\\image\\" + data[i][i1] + ".png"));
                imageLabel.setBounds(50 + 100 * i1, 90 + 100 * i, 100, 100);
                getContentPane().add(imageLabel);
            }
        }


        JLabel backguound = new JLabel(new ImageIcon("D:\\document\\JAVA_Project\\Practise\\day04\\src\\com\\itheima\\image\\background.png"));
        backguound.setBounds(26, 30, 450, 484);
        getContentPane().add(backguound);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    /**
     * 按键按下
     * @param e the event to be processed
     */
    @Override
    public void keyPressed(KeyEvent e) {
        int keyChar = e.getKeyChar();
        System.out.println("按下的键：" + keyChar);
        if (e.getKeyChar() == KeyEvent.VK_LEFT){
            System.out.println("左移动业务执行");
        } else if (e.getKeyChar() == KeyEvent.VK_UP) {
            System.out.println("上移动业务执行");
        }else if (e.getKeyChar() == KeyEvent.VK_RIGHT) {
            System.out.println("右移动业务执行");
        }else if (e.getKeyChar() == KeyEvent.VK_DOWN) {
            System.out.println("下移动业务执行");
        }
    }

    /**
     * 按键松开
     * @param e the event to be processed
     */
    @Override
    public void keyReleased(KeyEvent e) {

    }
}
